package com.siwei.mes.controller.laboratory;

import cn.jiguang.common.utils.StringUtils;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.entity.DownloadLaboratoryEntity;
import com.siwei.mes.entity.UploadLaboratoryEntity;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.concrete.batch.ConcreteKsdj;
import com.siwei.mes.entity.experiment.concrete.batch.ConcreteKyqd;
import com.siwei.mes.entity.experiment.concrete.batch.ConcreteKzqd;
import com.siwei.mes.entity.mixproportion.MixProportion;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.ExperimentDetailMapper;
import com.siwei.mes.mapper.mixProportion.MixProportionMapper;
import com.siwei.mes.service.experiment.ExperimentService;
import com.siwei.mes.service.laboratory.UploadAndDownloadAssociationService;
import com.siwei.mes.sync.config.SyncProcessorService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 提供前台调用  上传下载协会
 */
@Slf4j
@RestController
@RequestMapping("uploadAssociation")
public class UploadAndDownloadAssociationController {

    @Resource
    private UploadAndDownloadAssociationService uploadAndDownloadAssociationService;
    @Resource
    private SyncProcessorService syncProcessorService;
    @Resource
    private ExperimentService experimentService;
    @Resource
    private MixProportionMapper mixProportionMapper;

    @ApiOperation(value = "上传上海协会数据")
    @PostMapping("/upload")
    public Json uploadLaboratory(@RequestBody UploadLaboratoryEntity entity) {
        if (entity.getExperimentId() == null || entity.getExperimentId().isEmpty()) {
            assertTrue("上传的试验委托id为空");
        }
        List<String> experimentIdList = entity.getExperimentId();
        log.info("手动推送试验委托，推送数据到协会中，试验台账集合：【{}】", experimentIdList);

        for (String experimentId : experimentIdList){
            //根据品控台账id 查询
            Experiment experiment = experimentService.loadById(Long.parseLong(experimentId));

            assertTrue(experiment == null, "未查询到品控对应的台账信息！台账编号 :" + experiment.getExperimentNo());

            if(!MaterialsEnum.CONCRETE.getCode().equals(experiment.getExperimentType())){
                continue;
            }

            assertTrue(StringUtils.isEmpty(experiment.getPhb()), "配合比不存在，上传失败！不允许上传至协会！台账编号 :" + experiment.getExperimentNo());

            assertTrue(experiment.getMoldingTime() == null, "成型日期异常，上传失败！台账编号 :" + experiment.getExperimentNo());

            /**根据配合比编号查询对应的龄期*/
            MixProportion mixProportion = mixProportionMapper.selectByPhbNo(experiment.getPhb());
            assertTrue(mixProportion == null, "配合比不存在，上传失败！不允许上传至协会！台账编号 :" + experiment.getExperimentNo());

            String proportionLq = mixProportion.getProportionLq();
            log.info("上传协会数据，根据配合比查询到的龄期：【{}】", proportionLq);

            assertTrue(org.apache.commons.lang.StringUtils.isBlank(proportionLq), "配合比龄期为空，上传失败！，不允许上传至协会！台账编号 :" + experiment.getExperimentNo());

          /*  List<ExperimentDetail> list = experimentDetailMapper.getList(new ExperimentDetail().setExperimentId(experiment.getId()));

            //龄期
            String lq = "";

            for (ExperimentDetail detail : list){
                //混凝土抗压
                if (detail.getTestProjectCode().equals(TestProjectEnum.CONCRETE_PARAM_KYQD.getCode())) {
                    ConcreteKyqd concreteKyqd = detail.getObjJson().toJavaObject(ConcreteKyqd.class);
                    if (concreteKyqd != null && org.apache.commons.lang.StringUtils.isNotBlank(concreteKyqd.getLq())) {
                        lq = concreteKyqd.getLq();
                    }else if (detail.getTestProjectCode().equals(TestProjectEnum.CONCRETE_PARAM_KSDJ.getCode())) {
                        ConcreteKsdj concreteKsdj = detail.getObjJson().toJavaObject(ConcreteKsdj.class);
                        if (concreteKsdj != null && org.apache.commons.lang.StringUtils.isNotBlank(concreteKsdj.getLq())) {
                            lq = concreteKsdj.getLq();
                        }
                    }else if (detail.getTestProjectCode().equals(TestProjectEnum.CONCRETE_PARAM_KZQD.getCode())) {
                        ConcreteKzqd concreteKzqd = detail.getObjJson().toJavaObject(ConcreteKzqd.class);
                        if (concreteKzqd != null && org.apache.commons.lang.StringUtils.isNotBlank(concreteKzqd.getLq())) {
                            lq = concreteKzqd.getLq();
                        }
                    }
                }
            }

            assertTrue(StringUtils.isEmpty(lq), "配合比龄期为空，上传失败！，不允许上传至协会！台账编号 :" + experiment.getExperimentNo());*/
        }

        /**这里有多个拆分成一个一个的推送  防止数据并发重复 反正都是队列一个一个处理一样的*/
        for (String experimentId : experimentIdList) {
            syncProcessorService.submitRequest(new UploadLaboratoryEntity().setExperimentId(Collections.singletonList(experimentId)).setUploadState(0));
        }
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "下载上海协会数据")
    @PostMapping("/download")
    public Json downloadLaboratory(@RequestBody DownloadLaboratoryEntity entity) {
        uploadAndDownloadAssociationService.downloadLaboratory(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "补全协会委托+样品+任务单关联表接口")
    @GetMapping("insetWtConsignWithSample")
    public Json insetWtConsignWithSample(@RequestParam("beginDate") String beginDate, @RequestParam("endDate") String endDate) {
        uploadAndDownloadAssociationService.insetWtConsignWithSample(beginDate, endDate);
        return Json.succ();
    }

    @ApiOperation(value = "同步协会历史混凝土抗渗/抗折台账信息")
    @GetMapping("synchronizeData")
    public Json synchronizeData(@RequestParam("beginDate") String beginDate, @RequestParam("endDate") String endDate) {
        uploadAndDownloadAssociationService.synchronizeData(beginDate, endDate);
        return Json.succ();
    }

}
